package ru.surfstudio.graduate.activity;

import ru.surfstudio.graduate.R;
import ru.surfstudio.graduate.handler.ChannelResultHandler;
import ru.surfstudio.graduate.service.HttpService;
import ru.surfstudio.graduate.task.ChannelSearchTask;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Messenger;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

public class SearchChannelActivity extends Activity {

	public static final int DIALOG_SEARCH = 0x1;
	
	private static final String TAG = "SearchChannelActivity";
	
	private ChannelSearchActivityState state;
	
	private EditText searchText;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_channel_activity);
		
		state = (ChannelSearchActivityState) getLastNonConfigurationInstance();
		if (state == null) {
			state = new ChannelSearchActivityState(this);
			
		} else {
			state.attach(this);
		}
		
		searchText = (EditText) findViewById(R.id.search_channel_search_text);
		
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		state.detach();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
			.toggleSoftInput(0, 0);
	}
	
	@Override
    public Object onRetainNonConfigurationInstance() {
        state.detach();
        return state;
    }
	
	@Override
	protected Dialog onCreateDialog(int id) {
		
		switch (id) {
			case DIALOG_SEARCH:
				ProgressDialog dialog = new ProgressDialog(this);
				
				dialog.setMessage(getString(R.string.load_message));
				dialog.setIndeterminate(true);
				dialog.setCancelable(false);
				return dialog;
		}
		
		return super.onCreateDialog(id);
	}
	
	public String getSearchTextStr() {
		return searchText.getText().toString();
	}
	
	public void seachChannels(View v) {
		String searchStr = getSearchTextStr();
		if (searchStr == null || searchStr.equals("")) {
			Toast.makeText(SearchChannelActivity.this, R.string.channel_result_load_error, 
					Toast.LENGTH_SHORT);
			Log.w(TAG, getString(R.string.channel_result_load_error));
		} else {
			
			Intent intent = new Intent(this, HttpService.class);
			intent.putExtra(HttpService.MARK_HANDLER, new Messenger(state.handler));
			intent.putExtra(HttpService.MARK_TASK, new ChannelSearchTask(searchStr));
			startService(intent);
		}
	}
	
    /** Activity state */
    private static class ChannelSearchActivityState {

        public ChannelResultHandler handler;
        
        public ChannelSearchActivityState(SearchChannelActivity activity) {
        	handler = new ChannelResultHandler(activity);
        }
        
        public void attach(SearchChannelActivity activity) {
        	handler.attach(activity);
        }
        
        public void detach() {
        	handler.detach();
        }

    }
	
	
}
